# !/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@作者 杨阳
@时间 2018-07-31 14:30:12
@说明 t_lesson表对应的routes文件
@注意事项
"""

from flask import Blueprint, request

from model.LessonModel import TLessonModel
from util.JsonUtil import classToDict, JsonEncoder
from util.SessionUtil import Mast_be_login_decorator
import json

lesson = Blueprint('lesson', __name__, url_prefix='/lesson')


@lesson.route("/grid/", methods=["POST"])
@Mast_be_login_decorator
def entity_grid():
    """
    查询实体类表格形式的数据
    :return: json 字符串
    """
    current_page = request.form.get("current_page")
    group_id = request.form.get("group_id")
    code = request.form.get("search_code")
    name = request.form.get("search_name")
    status = request.form.get("search_status")
    allowday = request.form.get("search_allowday")
    mark = request.form.get("search_mark")
    cost = request.form.get("search_cost")
    hour = request.form.get("search_hour")
    isopen = request.form.get("search_isopen")
    objectable = request.form.get("search_objectable")
    maker = request.form.get("search_maker")
    remark = request.form.get("search_remark")
    kj_type = request.form.get("search_kj_type")
    create_time = request.form.get("search_create_time")
    enter_url = request.form.get("search_enter_url")
    training_target = request.form.get("search_training_target")
    training_comment = request.form.get("search_training_comment")
    training_manner = request.form.get("search_training_manner")
    handbacktime = request.form.get("search_handbacktime")
    ispermit_preview = request.form.get("search_ispermit_preview")
    ismini = request.form.get("search_ismini")
    terminaltype = request.form.get("search_terminaltype")
    path = request.form.get("search_path")
    coursewareprovider = request.form.get("search_coursewareprovider")
    publishdate = request.form.get("search_publishdate")
    versioncode = request.form.get("search_versioncode")
    languagetype = request.form.get("search_languagetype")
    keyword = request.form.get("search_keyword")
    author = request.form.get("search_author")
    selfnavigator = request.form.get("search_selfnavigator")
    selfindexlink = request.form.get("search_selfindexlink")
    teacher = request.form.get("search_teacher")
    lesson_img = request.form.get("search_lesson_img")
    urluptype = request.form.get("search_urluptype")
    score = request.form.get("search_score")
    resourceids = request.form.get("search_resourceids")
    kj_count = request.form.get("search_kj_count")
    file = request.form.get("search_file")
    return TLessonModel.query_entity_grid(group_id, code, name, status, allowday, mark, cost, hour, isopen, objectable,
                                          maker, remark, kj_type, create_time, enter_url, training_target,
                                          training_comment, training_manner, handbacktime, ispermit_preview, ismini,
                                          terminaltype, path, coursewareprovider, publishdate, versioncode,
                                          languagetype, keyword, author, selfnavigator, selfindexlink, teacher,
                                          lesson_img, urluptype, score, resourceids, kj_count, file, current_page)


@lesson.route("/save/", methods=["POST"])
@Mast_be_login_decorator
def save_entity():
    """
    保存实体
    :return: json 字符串
    """
    code = request.form.get("code")
    name = request.form.get("name")
    status = request.form.get("status")
    allowday = request.form.get("allowday")
    mark = request.form.get("mark")
    cost = request.form.get("cost")
    hour = request.form.get("hour")
    isopen = request.form.get("isopen")
    objectable = request.form.get("objectable")
    maker = request.form.get("maker")
    creator = request.form.get("creator")
    remark = request.form.get("remark")
    kj_type = request.form.get("kj_type")
    create_time = request.form.get("create_time")
    enter_url = request.form.get("enter_url")
    training_target = request.form.get("training_target")
    training_comment = request.form.get("training_comment")
    training_manner = request.form.get("training_manner")
    handbacktime = request.form.get("handbacktime")
    ispermit_preview = request.form.get("ispermit_preview")
    ismini = request.form.get("ismini")
    terminaltype = request.form.get("terminaltype")
    path = request.form.get("path")
    coursewareprovider = request.form.get("coursewareprovider")
    publishdate = request.form.get("publishdate")
    versioncode = request.form.get("versioncode")
    languagetype = request.form.get("languagetype")
    keyword = request.form.get("keyword")
    author = request.form.get("author")
    selfnavigator = request.form.get("selfnavigator")
    selfindexlink = request.form.get("selfindexlink")
    teacher = request.form.get("teacher")
    group_id = request.form.get("group_id")
    lesson_img = request.form.get("lesson_img")
    urluptype = request.form.get("urluptype")
    score = request.form.get("score")
    resourceids = request.form.get("resourceids")
    kj_count = request.form.get("kj_count")
    file = request.form.get("file")
    if id and id != '':
        return TLessonModel.update_entity(id, code, name, status, allowday, mark, cost, hour, isopen, objectable, maker,
                                          creator, remark, kj_type, create_time, enter_url, training_target,
                                          training_comment, training_manner, handbacktime, ispermit_preview, ismini,
                                          terminaltype, path, coursewareprovider, publishdate, versioncode,
                                          languagetype, keyword, author, selfnavigator, selfindexlink, teacher,
                                          group_id, lesson_img, urluptype, score, resourceids, kj_count, file)
    else:
        return TLessonModel.add_entity(code, name, status, allowday, mark, cost, hour, isopen, objectable, maker,
                                       remark, kj_type, create_time, enter_url, training_target, training_comment,
                                       training_manner, handbacktime, ispermit_preview, ismini, terminaltype, path,
                                       coursewareprovider, publishdate, versioncode, languagetype, keyword, author,
                                       selfnavigator, selfindexlink, teacher, lesson_img, urluptype, score, resourceids,
                                       kj_count, file)


@lesson.route("/delete/<id>", methods=["DELETE"])
@Mast_be_login_decorator
def delete_entity(id):
    """
    根据id删除实体类
    :return: json 字符串
    """
    return TLessonModel.delete_entity(id)


@lesson.route("/get/<id>", methods=["GET"])
@Mast_be_login_decorator
def get_entity_byId(id):
    """
    根据id获得实体
    :return: json 字符串
    """
    lesson = TLessonModel.get_entity_byId(id)
    result = {"success": True, "lesson": classToDict({lesson})}
    return json.dumps(result, cls=JsonEncoder)
